﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Ymatou.API.Common;
using Ymatou.CommonService;
using Ymatou.Evaluate.Seller.Model.ApiModel.Request.Query;
using Ymatou.Evaluate.Seller.Model.ApiModel.Response.Query;
using Ymatou.Evaluate.Seller.Model.Dto;
using Ymatou.Evaluate.Service.Seller.Implement;
using Ymatou.PerfMonitorClient;

namespace Evaluate.Api.Controllers
{
    public class QueryController : ApiController
    {
        /// <summary>
        /// 获取商品评价列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/query/GetOrderProductsByName")]
        [HttpGet]
        public BaseResponseModel<GetOrderProductsByNameResponse> GetOrderProductsByName([FromUri]GetOrderProductsByNameRequest request)
        {
            using (MethodMonitor.New("api/query/GetOrderProductsByName"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();

                    var response = new SellerQueryService().GetOrderProductsByName(request.SellerId, request.ProductName, request.MaxCount);
                    return new SuccessResponseModel<GetOrderProductsByNameResponse>(response);
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/query/GetOrderProductsByName业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetOrderProductsByNameResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/query/GetOrderProductsByName异常", ex);
                    ApplicationLog.Info(string.Format("api/query/GetOrderProductsByName异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetOrderProductsByNameResponse>(ex.Message);
                }
            }
        }

        /// <summary>
        /// 获取低于基准分值评论总数
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/query/GetEvaluateCount")]
        [HttpGet]
        public BaseResponseModel<GetEvaluateCountResponse> GetEvaluateCount([FromUri]GetEvaluateCountRequest request)
        {
            using (MethodMonitor.New("api/query/GetEvaluateCount"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    var response = new SellerQueryService().GetEvaluateCount(request.SellerId, request.Score);
                    return new SuccessResponseModel<GetEvaluateCountResponse>(response);
                   
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/query/GetEvaluateCount业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetEvaluateCountResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/query/GetEvaluateCount异常", ex);
                    ApplicationLog.Info(string.Format("api/query/GetEvaluateCount异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetEvaluateCountResponse>(ex.Message);
                }
            }
        }


        /// <summary>
        /// 获取评价列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("api/query/GetEvaluateList")]
        [HttpGet]
        public BaseResponseModel<GetEvaluateListResponse> GetEvaluateList([FromUri]GetEvaluateListRequest request)
        {
            using (MethodMonitor.New("api/query/GetEvaluateList"))
            {
                try
                {
                    if (request == null) throw new BussniessException("-1", "请求参数不能传入null");
                    request.Validate();
                    var response = new SellerQueryService().GetEvaluateList(request);
                    return new SuccessResponseModel<GetEvaluateListResponse>(response);
                }
                catch (BussniessException ex)
                {
                    ApplicationLog.Info(string.Format("api/query/GetEvaluateList业务异常：Code={0},Msg={1}", ex.ErrorCode, ex.ErrorMsg));
                    return new BussniessErrorResponseModel<GetEvaluateListResponse>(int.Parse(ex.ErrorCode), ex.ErrorMsg);
                }
                catch (Exception ex)
                {
                    ApplicationLog.Error("api/query/GetEvaluateList异常", ex);
                    ApplicationLog.Info(string.Format("api/query/GetEvaluateList异常请求参数:{0}",
                        JsonConvert.SerializeObject(request)));
                    return new SystemErrorResponseModel<GetEvaluateListResponse>(ex.Message);
                }
            }
        }


       
    }
}
